from sqlalchemy import Column, Integer, Index
from sqlalchemy.orm.session import Session

from airflow.models.base import Base
from airflow.utils.session import provide_session

'''
condition 0 失败 1 启动超时 2 任务运行超时
'''


class TaskAlert(Base):
    id = Column(Integer, primary_key=True)
    task_id = Column(Integer)
    alert_id = Column(Integer)
    condition = Column(Integer)

    __tablename__ = "idata_task_alert"

    __table_args__ = (
        Index('task_id', task_id, alert_id, condition),
    )

    @staticmethod
    @provide_session
    def query(task_id, session: Session = None):
        return session.query(TaskAlert).filter(TaskAlert.task_id == task_id).all()

    @provide_session
    def insert(self, session: Session = None):
        session.add(self)

    @provide_session
    def delete(self, session: Session = None):
        session.query(TaskAlert).filter(TaskAlert.id == self.id).delete()
